package com.tozed.android.launcher.simpleui;

import java.util.HashMap;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;

public class SimpleApplicationInfo extends SimpleItemInfo
{
	public ComponentName componentName;
	public long firstInstallTime;
	public boolean isNormalHome = false;
	public boolean isPresetApp = false;
	
	public static final int DOWNLOADED_FLAG = 1;
	public static final int UPDATED_SYSTEM_APP_FLAG = 2;
    int flags = 0;

	public SimpleApplicationInfo(){
		
	}

	public SimpleApplicationInfo(PackageManager pm,ResolveInfo resolveInfo, SimpleIconCache iconCache,
			HashMap<Object, CharSequence> map)
	{
		String strPkgName = resolveInfo.activityInfo.applicationInfo.packageName;
	    componentName = new ComponentName(strPkgName, resolveInfo.activityInfo.name);
	    intent = new Intent(Intent.ACTION_MAIN);
	    intent.addCategory(Intent.CATEGORY_LAUNCHER);
	    intent.setComponent(new ComponentName(strPkgName, resolveInfo.activityInfo.name));
	    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
	    iconCache.getTitleAndIcon(this, resolveInfo, map);
	    
	    try
	    {
	      firstInstallTime = pm.getPackageInfo(strPkgName, 0).firstInstallTime;
	      if (SimpleFolder.CLASS_NAME.equals(resolveInfo.activityInfo.name))
	      {
	        isNormalHome = true;
	        firstInstallTime = Long.MAX_VALUE;
	      }
	      
	      int appFlags = pm.getApplicationInfo(strPkgName, 0).flags;
          if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
              flags |= DOWNLOADED_FLAG;
              if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
                  flags |= UPDATED_SYSTEM_APP_FLAG;
              }
          }
	    }catch (NameNotFoundException e){
	        e.printStackTrace();
	    }
	}

	public SimpleApplicationInfo(SimpleItemInfo itemInfo){
		super(itemInfo);
	}

	public ComponentName getComponentName(){
		return this.componentName;
	}

	public Bitmap getIcon(SimpleIconCache paramSimpleIconCache){
		if (mIcon == null)
			updateIcon(paramSimpleIconCache);
		return this.mIcon;
	}

	public void setTitle(CharSequence title){
		this.title = title;
	}

	public void updateIcon(SimpleIconCache iconCache){
		this.mIcon = iconCache.getIcon(this.intent);
		iconCache.isDefaultIcon(this.mIcon);
	}
}
